<?php


namespace app\backend\controller\v1;

use app\backend\validate\BaseSiteValidate;
use app\common\model\BaseSite as BaseSiteModel;
use think\exception\ValidateException;

class BaseSite extends BaseController
{
    /**
     * 联系我们列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $model = new BaseSiteModel();
        $data = $model->find(1);
        if (!empty($data['logo']))
            $data['logo'] = env('APP.YUMING') . $data['logo'];
        return sucessMsg(200, '网站基础设置获取成功！', $data);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @return \think\Response
     */
    public function edit()
    {
        //  接收id
        $id = input('get.id/d');
        if (empty($id)) {
            return errMsg(422, 'id不能为空');
        }
        $data = BaseSiteModel::where('id', $id)->find();
        if (!empty($data['logo']))
            $data['logo'] = env('APP.YUMING') . $data['logo'];
        if (empty($data))
            return errMsg(422, '无效id');
        return sucessMsg(200, '获取成功！', $data);
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update()
    {
        $data = input();
        try {
            //  验证
            validate(BaseSiteValidate::class)->scene('update')->check($data);
            if (empty($data['logo'])) {
                return errMsg(400, '请上传logo图');
            }
            $model = new BaseSiteModel();
            $cate = $model->where('id', $data['id'])->find();
            if ($cate->save($data)) {
                return sucessMsg(200, '修改成功！');
            }
            return errMsg(500, '修改失败！');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }
}